package com.arpa.ntocc.common.mapper;

import com.arpa.ntocc.common.domain.dto.ResourceDTO;
import com.arpa.ntocc.common.domain.entity.Resource;
import com.arpa.ntocc.common.domain.vo.ResourceVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 资源 Mapper 接口
 * </p>
 *
 * @author LEO
 * @since 2019-08-02
 */
public interface ResourceMapper extends BaseMapper<Resource> {
    /**
     * <p>
     * 查询 : 根据ResourcesDTO值查询资源列表，分页显示
     * 注意!!: 如果入参是有多个,需要加注解指定参数名才能在xml中取值
     * </p>
     *
     * @param page         分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位(你可以继承Page实现自己的分页对象)
     * @param resourceDTO 查询参数
     * @return 分页对象
     */
    Page<ResourceVO> select(Page<ResourceVO> page, ResourceDTO resourceDTO);


    /**
     * 根据角色Ids获取所有授权资源
     * @param roleIds
     * @return 授权资源
     */
    List<ResourceVO> getResourcesByRoleIds(@Param("roleIds") List<Object> roleIds);

    /**
     * 根据用户code查询资源列表
     *
     * @param partyCode
     * @return
     */
//   @Select("select distinct * from sys_resource rs join sys_role_resource rr on rs.code=rr.resource_code join sys_party_role pr on pr.role_code=rr.role_code where pr.party_code= #{partyCode}  and rr.is_deleted = 0 and pr.is_deleted = 0 and rs.is_deleted = 0  order by rs.sort ")
    @Select("SELECT * FROM sys_resource rs \n" +
            "inner JOIN (\n" +
            "\n" +
            "select rr.resource_code from  sys_role_resource rr \n" +
            "JOIN sys_party_role pr ON pr.role_code = rr.role_code \n" +
            "WHERE pr.party_code = #{partyCode}  \n" +
            "AND rr.is_deleted = 0\n" +
            "AND pr.is_deleted = 0\n" +
            "GROUP BY rr.resource_code\n" +
            ") temp on rs.code = temp.resource_code\n" +
            "AND rs.is_deleted = 0 \n" +
            "ORDER BY rs.sort ")
    List<Resource> listByPartyCode(String partyCode);


    /**
     * 根据用户code和机构code查询资源列表
     * @param partyCode 用户code
     * @param branchCode 机构code
     * @return
     */
    @Select("SELECT * FROM sys_resource rs \n" +
            "inner JOIN (\n" +
            "\n" +
            "select rr.resource_code from  sys_role_resource rr \n" +
            "JOIN sys_party_role pr ON pr.role_code = rr.role_code \n" +
            "WHERE pr.party_code = #{partyCode}  \n" +
            "AND rr.is_deleted = 0\n" +
            "AND pr.is_deleted = 0\n" +
            "GROUP BY rr.resource_code\n" +
            ") temp on rs.code = temp.resource_code\n" +
            "AND rs.is_deleted = 0  and rs.branch_code=#{branchCode}  \n" +
            "ORDER BY rs.sort ")
    List<Resource> listByPartyCodeAndBranchCode(@Param("partyCode") String partyCode, String branchCode);

    @Select("SELECT s.code,sb.code as codeParentCodeOne,sr.code as codeParentCodeTwo\n" +
            "FROM `sys_resource` s  left join sys_resource sb on s.parent_code = sb.code\n" +
            " left join sys_resource sr on sb.parent_code = sr.code where s.\n" +
            "name = #{value}")
    List<ResourceVO> getList(String value);
}
